#include "Particle.h"

using namespace PHYOgre;

Particle::Particle()
	:mVelocity(Vector3::ZERO)
	,mAcceleration(Vector3::ZERO)
	,mInverseMass(0.0f)
	,mDampling(DEFAULT_DAMPLING)
	,mForceAccumulator(Vector3::ZERO)
	
{

}

void Particle::SetNode(Ogre::Node *iNode)
{
	mNode = iNode;
}
void Particle::SetPosition(const Vector3& iPosition)
{
	mNode->setPosition(iPosition);
}
Vector3 Particle::GetPosition() const
{
	return mNode->getPosition();
}
void Particle::SetVelocity(const Vector3& iVelocity)
{
	mVelocity = iVelocity;
}
Vector3 Particle::GetVelocity() const
{
	return mVelocity;
}
void Particle::SetAcceleration(const Vector3& iAcceleration)
{
	mAcceleration = iAcceleration;
}
	
void Particle::SetDampling(const Real& iDampling)
{
	mDampling = iDampling;
}
void Particle::SetMass(const Real& iMass)
{
	assert(iMass >=0.0f);

	if (iMass <= MIN_MASS_VALUE)
		mInverseMass = MAX_MASS_VALUE;
	else
		mInverseMass = 1.0f / iMass;

}
Real Particle::GetInverseMass() const
{
	return mInverseMass;
}
void Particle::SetInverseMass(const Real& iInverseMass)
{
	assert(iInverseMass >0.0f);
	mInverseMass = iInverseMass;
}

void Particle::Integrate(const Real& iDT)
{
	if(iDT <= 0.0f)
		return;

	mNode->translate(mVelocity * iDT);

	Vector3 acc = mAcceleration;
	acc += mForceAccumulator * mInverseMass;

	mVelocity += acc * iDT;

	mVelocity *=  Ogre::Math::Pow(mDampling , iDT);
	ClearForceAccumulator();
}
bool Particle::HasFiniteMass() const
{
	return mInverseMass == 0 ? false: true;
}
void Particle::ClearForceAccumulator()
{
	mForceAccumulator = Vector3::ZERO;
}

void Particle::AddForce(const Vector3& iForce)
{
	mForceAccumulator += iForce;
}

const Ogre::Sphere& Particle::GetBoundingSphere() const
{
	return mObject->getWorldBoundingSphere();
}
const Ogre::MovableObject& Particle::GetObject() const
{
	return *mObject;
}
void Particle::SetShape(Shape* iShape)
{
	
}
const Shape* Particle::GetShape() const
{
	return nullptr;
}

